<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <canvas id="rect" width="500" , height="500" style="border: 2px solid #0f0;"></canvas>
  <script>
    const canvas = document.getElementById('rect')
    const ctx = canvas.getContext('2d')


    // 1. 画坐标系
    ctx.beginPath()
    ctx.moveTo(100, 100)
    ctx.lineTo(100, 400)
    ctx.lineTo(400, 400)
    ctx.stroke()
    ctx.closePath()

    // 2. 画矩形
    // 必需重新开始，负责会与上图（坐标系）冲突
    ctx.beginPath()
    ctx.rect(200, 200, 20, 200)
    ctx.fillStyle = 'gold'
    ctx.fill()
    // ctx.stroke()
    ctx.closePath()
  </script>
</body>

</html>